package com.wudao.ncmplay.util;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;
import com.wudao.ncmplay.controller.BaseController;
import com.wudao.ncmplay.pojo.BaseEntity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FastAutoGeneratorUtil
{
    /**
     * 数据源配置
     */
    private static final DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig
            .Builder("jdbc:mysql://localhost:3306/ncm_play?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false",
            "root", "123456")
            .typeConvert(new MySqlTypeConvert())
            .keyWordsHandler(new MySqlKeyWordsHandler());

    /**
     * 执行 run
     */
    public static void main(String[] args)
    {
        List<String> tables = new ArrayList<>();
        tables.add("ncm_play_file");

        FastAutoGenerator.create(DATA_SOURCE_CONFIG)
                // 全局配置
                .globalConfig(builder -> builder.author("middle2boy")
                        .outputDir("E:\\git\\ncm-play\\src\\main\\java")
                        .enableSwagger()
                        .commentDate("yyyy-MM-dd")
                        .dateType(DateType.ONLY_DATE)
                        .build())
                // 包配置
                .packageConfig(builder -> builder.parent("com.wudao.ncmplay")
                        .entity("pojo")
                        .controller("controller")
                        .service("service")
                        .serviceImpl("service.impl")
                        .mapper("mapper")
                        .xml("mapper.xml")
                        .pathInfo(Collections.singletonMap(OutputFile.xml, "E:\\git\\ncm-play\\src\\main\\resources\\mapper"))
                        .build())
                // 策略配置
                .strategyConfig(builder -> builder.addInclude(tables)
                        .addTablePrefix("_")
                        .entityBuilder().superClass(BaseEntity.class)
                        .enableLombok()
                        .enableRemoveIsPrefix()
                        .enableTableFieldAnnotation()
                        .enableActiveRecord()
                        .versionColumnName("version")
                        .versionPropertyName("version")
                        .naming(NamingStrategy.underline_to_camel)
                        .columnNaming(NamingStrategy.underline_to_camel)
                        .addTableFills(new Column("create_time", FieldFill.INSERT))
                        .addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE))
                        .idType(IdType.ASSIGN_UUID)
                        .formatFileName("%sEntity")
                        .serviceBuilder()
                        .formatServiceFileName("%sService")
                        .formatServiceImplFileName("%sServiceImpl")
                        .controllerBuilder().superClass(BaseController.class)
                        .formatFileName("%sController")
                        .mapperBuilder().superClass(BaseMapper.class)
                        .enableMapperAnnotation()
                        .enableBaseResultMap()
                        .enableBaseColumnList()
                        .formatMapperFileName("%sMapper")
                        .formatXmlFileName("%sMapper")
                        .build())
                .templateEngine(new FreemarkerTemplateEngine())   // 模板引擎配置Freemarker,默认Velocity
                .execute();
    }
}
